package com.itheima.rocketmq.rocketmqdemo.base.product;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.concurrent.TimeUnit;

/**
 * 发送同步消息
 * @description:
 * @author: ycc
 * @time: 2021/3/24
 */
public class SyncProduct {
    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876;127.0.0.1:9877");
//        3.启动producer
        producer.start();
        for (int i=0;i<10;i++){
//        4.创建消息对象，指定主题Topic、Tag和消息体
            Message msg = new Message("base","Tag1",("余长城短信"+i).getBytes());
//        5.发送消息
            SendResult result = producer.send(msg,1000*30);
            String msgId = result.getMsgId();
            SendStatus status = result.getSendStatus();
            int queueId = result.getMessageQueue().getQueueId();
            String offsetMsgId = result.getOffsetMsgId();
            long queueOffset = result.getQueueOffset();
            String regionId = result.getRegionId();
            System.out.println("result：------"+result+"消息id：------"+msgId+"，状态：------"+status+"，序列id：------"+queueId+"，offsetMsgId：------"+offsetMsgId+"，queueOffset：------"+queueOffset+"，regionId：------"+regionId);
            TimeUnit.SECONDS.sleep(1);
        }
//        6.关闭生产者producer
        producer.shutdown();
    }
}


